// Code Behind for propertycdialog.mxml
      	import flash.events.Event;
      	import flash.events.IOErrorEvent;
      	import flash.events.ProgressEvent;
      	import flash.events.SecurityErrorEvent;
      	import flash.net.FileReference;
      	import flash.net.FileReferenceList;
      	import flash.net.URLLoader;
      	import flash.net.URLRequest;
      	
      	import jcrweb.as3.FlexUtil;
      	import jcrweb.as3.RequestUtil;
      	import jcrweb.as3.LanguageUtil;
      	
      	import mx.collections.XMLListCollection;
      	import mx.controls.Alert;
      	import mx.managers.PopUpManager;
		
		private var loadedNums:Array = new Array();
		private var loadedFileIds:Array = new Array();
		private var loadedFileNames:Array = new Array();
		
		private var pendingFiles:Array;
		private var uploadedFileName:Array;
		private var fileList:FileReferenceList;
		private var uploadUrl:URLRequest;
		private var uploadingFlag:int = 0;
		
		public var allVersion:String = "0";
		public var showingVersion:String = "0";
		
		//===============init seting ======================
		[Bindable]
		private var strPath:String = "";
		[Bindable]
		private var strPathName:String = "";
		[Bindable]
		private var strId:String = "";
		[Bindable]
		private var verlstdata:XMLListCollection;
		[Bindable]
		private var languageUtil:LanguageUtil;
		private var strName:String = "";
		private var strNote:String = "";
		private var strDate:String = "";
		[Bindable]
		[Embed(source='../WebContent/images/attent.gif')]
		private var iconAlert:Class;
		[Bindable]
		[Embed(source='../WebContent/images/error.gif')]
		private var iconError:Class;
		
		
		public function setId(strId:String):void{
			this.strId = strId;
		}
		public function setPath(path:String):void{
			this.strPath = path;
		}
		public function setPathName(pathName:String):void{
			this.strPathName = pathName;
		}
		public function setName(inputStr:String):void{
			input_name.text = inputStr;
			strName = inputStr;
		}
		public function setCreaterCode(inputStr:String):void{
			input_createrCode.text = inputStr;
		}
		public function setCreaterName(inputStr:String):void{
			input_createrName.text = inputStr;
		}
		public function setDatetime(inputStr:String):void{
			input_datetime.text = inputStr;
			strDate = inputStr;
		}
		public function setNote(inputStr:String):void{
			input_note.text = inputStr;
			strNote = inputStr;
		}
		public function setFiles(fileId:String,fileName:String,num:int):void{
			if(fileId != "0"){
				this["txt"+num].visible = false;
				this["pgb"+num].visible = false;
				this["btn"+num].visible = true;
				this["btn"+num].label = fileName;

				this.loadedFileIds.push(fileId);
				this.loadedFileNames.push(fileName);
				this.loadedNums.push(num.toString());
			}
		}
		private function init():void{
			this.fileList     = new FileReferenceList();		
			this.fileList.addEventListener(Event.SELECT,selectHandler);		
			var header:URLRequestHeader = new URLRequestHeader("Content-Type", "multipart/form-data");
			this.uploadUrl = new URLRequest(RequestUtil.INITCONTENT_URL+"?path="+this.strPath);
			this.uploadUrl.method = URLRequestMethod.POST;
			this.uploadUrl.requestHeaders.push(header);
		
			languageUtil = LanguageUtil.getInstance();
		}
		private function selectHandler(event:Event):void{
			var len:int = event.currentTarget.fileList.length;
			if(len > (10 - this.loadedNums.length)){
				len = 10 - this.loadedNums.length;
			}
			this.pendingFiles = new Array();
			this.uploadedFileName = new Array();
			
			for(var j:int=0;j<10;j++){
				this["txt"+(j+1)].text = "";
			}
			for(var i:int=0;i<len;i++){
				this.addPengingFiles(event.currentTarget.fileList[i]);
				for(var z:int=0;z<10;z++){
					if(this["txt"+(z+1)].visible&&this["txt"+(z+1)].text==""){
						this["txt"+(z+1)].text=event.currentTarget.fileList[i].name;
						break;
					}
				}
			}
		}
		private function addPengingFiles(file:FileReference):void{
			this.pendingFiles.push(file);
		}
		//===============init seting ======================
		
		//===============file upload ======================
		private function selected():void{
			this.fileList.browse();
		}
		private function getTypes():Array{
			//var allTypes:Array = new Array();
			return null;
		}
		private function doOnComplete():void{
			uploadAlert.visible = false;
			this.uploadingFlag = 2;
			var para:String = new String();
			para +="&id="+this.strId;
			para +="&path="+input_path.text;
			para +="&name="+input_name.text;
			para +="&createrCode="+input_createrCode.text;
			para +="&createrName="+input_createrName.text;
			para +="&datetime="+input_datetime.text;
			para +="&note="+input_note.text;
			if(this.uploadedFileName!=null){
				for(var i:int=0;i<this.uploadedFileName.length;i++){
					var fileName:String = this.uploadedFileName[i];
					var len:int = 0;
					for(var j:int=0;j<10;j++){
						if(this["txt"+(j+1)].visible&&this["txt"+(j+1)].text == fileName){
							len = j;
							break;
						}
					}
					para +="&fileName"+(len+1)+"="+fileName;
				}
			}
			for(var k:int=0;k<10;k++){
						if(this["btn"+(k+1)].visible&&this["btn"+(k+1)].label != ""){
							para +="&fileName"+(k+1)+"="+this["btn"+(k+1)].label;
						}
			}
			para =  encodeURI(para);
			para =  encodeURI(para);
			var url:String = RequestUtil.UPDATACON_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,complete);
		}
		private function complete(event:Event):void {
			var loader:URLLoader = URLLoader(event.target); 
			var isError:String = FlexUtil.getTag(loader.data,FlexUtil.IS_ERROR);
			if(isError == "true" ){
				var errorMes:String = FlexUtil.getTag(loader.data,FlexUtil.ERROR_MES_TAG);
				Alert.show("Create directory faild!"+errorMes,"Error",0,null,null,iconError);
			}
			closeDialog();
		}
		private function closeDialog():void{
			PopUpManager.removePopUp(this);
		}
		private function upload():void{
			for(var i:int;i<this.pendingFiles.length;i++){
				var file:FileReference = FileReference(this.pendingFiles[i]);
				file.addEventListener(Event.COMPLETE,completeHandler);
				file.addEventListener(ProgressEvent.PROGRESS,progressHandler);
				file.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
				file.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
				file.upload(uploadUrl);
			}
		}
		private function completeHandler(event:Event):void{
			var file:FileReference = FileReference(event.target);
			this.uploadedFileName.push(file.name);
			this.removePendingFiles(file.name);
		}
		private function removePendingFiles(filename:String):void{
			for(var i:int;i<this.pendingFiles.length;i++){
				if(this.pendingFiles[i].name == filename){
					this.pendingFiles.splice(i,1);
					break;
				}
			}
			if(this.pendingFiles.length == 0){
				doOnComplete();
			}
		}		
		private function progressHandler(event:ProgressEvent):void{
			var file:FileReference = FileReference(event.target);
			for(var i:int=0;i<10;i++){
				if(this["txt"+(i+1)].text == file.name){
					var fileUploadPercent:uint = (event.bytesLoaded / event.bytesTotal) * 100;
					this["pgb"+(i+1)].setProgress(fileUploadPercent,100);
				}
			}
		}
		private function ioErrorHandler(event:IOErrorEvent):void{
			var file:FileReference = FileReference(event.target);
			Alert.show("IOErrorException:"+file.name+" event:"+event.toString(),"Error",0,null,null,iconError);
			file.cancel();
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void{
			var file:FileReference = FileReference(event.target);
			Alert.show("SecurityErrorException:"+file.name+" event:"+event.toString(),"Error",0,null,null,iconError);
			file.cancel();
		}
		private function removeFiles(strNum:String):void{
			if(this.uploadingFlag == 0){
				for(var i:int;i<this.loadedNums.length;i++){
					if(this.loadedNums[i] == strNum){
						this.loadedFileNames.splice(i,1);
						this.loadedFileIds.splice(i,1);

						this["txt"+strNum].visible = true;
						this["pgb"+strNum].visible = true;
						this["btn"+strNum].visible = false;
						
						this.loadedNums.splice(i,1);
						break;
					}
				}
			}
			if(this.uploadingFlag == 1){
				for(var j:int;j<this.pendingFiles.length;j++){
					var file:FileReference = FileReference(this.pendingFiles[j]);
					if(file.name == this["txt"+strNum].text){
						file.cancel();
						this.pendingFiles.splice(i,1);
						break;
					}
				}
				this["pgb"+strNum].setProgress(0,100);
				
				if(this.pendingFiles.length == 0){
					doOnComplete();
				}				
			}
		}
		//===============file upload ======================
		private function submit():void{
			if(this.uploadingFlag == 1){
				Alert.show("The files are uploading now! Cannot submit.","Attention",0,null,null,iconAlert);
				return;
			}
			if(input_name.text == ""){
				Alert.show("Please input name.","Error",0,null,null,iconError);
				return;
			}
			if(this.pendingFiles!=null && this.pendingFiles.length > 0){
				this.uploadingFlag = 1;
				this.input_createrCode.editable = false;
				this.input_createrName.editable = false;
				this.input_datetime.editable = false;
				this.input_name.editable = false;
				this.input_note.editable = false;
				//uploading
				upload();
				uploadAlert.visible = true;
			}else{
				doOnComplete();
			}
		}
		private function close():void{
			this.cancel();
			this.closeDialog();
		}
		private function cancel():void{
			uploadAlert.visible = false;
			this.uploadingFlag = 0;
			if(this.pendingFiles!=null){
				for(var i:int;i<this.pendingFiles.length;i++){
					var file:FileReference = FileReference(this.pendingFiles[i]);
					file.cancel();
				}
			}
			this.pendingFiles = null;
			for(var j:int=0;j<10;j++){
				if(this["txt"+(j+1)].visible){
					this["txt"+(j+1)].text = "";
					this["pgb"+(j+1)].setProgress(0,100);
				}
			}
			this.input_createrCode.editable = true;
			this.input_createrName.editable = true;
			this.input_datetime.editable = true;
			this.input_name.editable = true;
			this.input_note.editable = true;
			this.input_name.text = strName;
			this.input_note.text = strNote;
			this.input_datetime.text = strDate;
		}
		private var downFile:FileReference = null;
		private function download(strNum:String):void{
			try{
				var strId:String = null;
				var strName:String = null;
				for(var i:int=0;i<this.loadedNums.length;i++){
					if(this.loadedNums[i] == strNum){
						strId = this.loadedFileIds[i];
						strName = this.loadedFileNames[i];
						break;
					}
				}
				if(strId == null){
					Alert.show("The file ["+strName+"] cannot be download.","Error",0,null,null,iconError);
					return;
				}
				downFile = new FileReference();
				downFile.addEventListener(Event.COMPLETE,downloadCompleteEvent);
				downFile.addEventListener(Event.CANCEL,downloadCompleteEvent);
				downFile.addEventListener(IOErrorEvent.IO_ERROR,downloadErrorEvent);
				downFile.addEventListener(ProgressEvent.PROGRESS,downloadProgressEvent);
				downFile.addEventListener(SecurityErrorEvent.SECURITY_ERROR,downloadErrorEvent);
				var downUrl:URLRequest = new URLRequest(RequestUtil.DOWNCON_URL+"?path="+this.strPath+"&fileId="+strId);
				downUrl.method = URLRequestMethod.POST;
				downloadAlert.visible = true;
				downFile.download(downUrl,strName);
			}catch(e:Error){
				Alert.show("Cannot download the file.","Error",0,null,null,iconError);
			}
		}
		private function downloadErrorEvent(event:Event):void {
			var file:FileReference = FileReference(event.target);
			Alert.show("Exception:"+file.name+" event:"+event.toString(),"Error",0,null,null,iconError);
		}
		private function downloadProgressEvent(event:ProgressEvent):void {	
			if(downloadMes.visible){
				downloadMes.visible = false;
			}else{
				downloadMes.visible = true;
			}
		}
		private function downloadCompleteEvent(event:Event):void {	
			downloadAlert.visible = false;
			downloadMes.visible = true;
		}
		
		private function delData():void{
			var para:String = new String();
			para +="&id="+this.strId;
			para +="&path="+input_path.text;
			para =  encodeURI(para);
			para =  encodeURI(para);
			var url:String = RequestUtil.DELCON_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,complete);
		}
		private function showver():void{
			if(this.allVersion == "0"){
				getAllVersion();
				showVerBtn.label = "Loading ......";
			}else{
				if(historyPanel.visible) 
					historyPanel.visible=false; 
				else 
					historyPanel.visible=true;
			}
		}
		public function getAllVersion():void{
			var para:String = new String();
			para +="&id="+this.strId;
			para +="&path="+input_path.text;
			para =  encodeURI(para);
			para =  encodeURI(para);
			var url:String = RequestUtil.CONVERSION_URL+para;
			RequestUtil.sendRequest(url,Event.COMPLETE,allVersionComplete);
		}
		private function allVersionComplete(event:Event):void {
			verlstdata = new XMLListCollection();
			var loader:URLLoader = URLLoader(event.target); 
			var resutlSize:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_SIZE);
			var allData:String = FlexUtil.getTag(loader.data,FlexUtil.RESULT_DATA)
			if(resutlSize != "0"){
				for(var j:int=Number(resutlSize);j>0;j--){
					var newnode:XML = new XML(FlexUtil.getTag(allData,new String(j)));
					verlstdata.addItem(newnode);
				}
				this.allVersion = resutlSize;
				this.showingVersion = resutlSize;
				this.showVerBtn.label = languageUtil.getData('as-history-tit')+"[ TotalVer:"+this.allVersion+" ShowingVer:"+this.showingVersion+" ]";
			}
		}
		private function showVersionInfo(event:Event):void{
			var nodeXml:XML = DataGrid(event.target).selectedItem as XML;
			this.loadedFileIds = new Array();
			this.loadedFileNames = new Array();
			this.loadedNums = new Array();
			for(var i:int=0;i<10;i++){
				this["txt"+(i+1)].visible = true;
				this["txt"+(i+1)].text="";
				this["pgb"+(i+1)].visible = true;
				this["btn"+(i+1)].visible = false;
				this["btn"+(i+1)].label="";
			}
			this.setName(nodeXml.name);
			this.setCreaterCode(nodeXml.createrCode);
			this.setCreaterName(nodeXml.createrName);
			this.setDatetime(nodeXml.datetime);
			this.setNote(nodeXml.note);
			this.setFiles(nodeXml.file1Id,nodeXml.file1Name,1);
			this.setFiles(nodeXml.file2Id,nodeXml.file2Name,2);
			this.setFiles(nodeXml.file3Id,nodeXml.file3Name,3);
			this.setFiles(nodeXml.file4Id,nodeXml.file4Name,4);
			this.setFiles(nodeXml.file5Id,nodeXml.file5Name,5);
			this.setFiles(nodeXml.file6Id,nodeXml.file6Name,6);
			this.setFiles(nodeXml.file7Id,nodeXml.file7Name,7);
			this.setFiles(nodeXml.file8Id,nodeXml.file8Name,8);
			this.setFiles(nodeXml.file9Id,nodeXml.file9Name,9);
			this.setFiles(nodeXml.file10Id,nodeXml.file10Name,10);
			
			this.showingVersion = nodeXml.ver;
			if(this.showingVersion == this.allVersion){
				this.init();
				this.input_datetime.editable = true;
				this.input_name.editable = true;
				this.input_note.editable = true;
				this.btnSubmit.enabled = true;
				this.btnCancel.enabled = true;
				this.btnDelete.enabled = true;
				
				this.btnSelected.enabled = true;
				
				this.btlDelFile1.enabled = true;
				this.btlDelFile2.enabled = true;
				this.btlDelFile3.enabled = true;
				this.btlDelFile4.enabled = true;
				this.btlDelFile5.enabled = true;
				this.btlDelFile6.enabled = true;
				this.btlDelFile7.enabled = true;
				this.btlDelFile8.enabled = true;
				this.btlDelFile9.enabled = true;
				this.btlDelFile10.enabled = true;
			}else{
				this.init();
				this.input_createrCode.editable = false;
				this.input_createrName.editable = false;
				this.input_datetime.editable = false;
				this.input_name.editable = false;
				this.input_note.editable = false;
				this.btnSubmit.enabled = false;
				this.btnCancel.enabled = false;
				this.btnDelete.enabled = false;
				
				this.btnSelected.enabled = false;
				
				this.btlDelFile1.enabled = false;
				this.btlDelFile2.enabled = false;
				this.btlDelFile3.enabled = false;
				this.btlDelFile4.enabled = false;
				this.btlDelFile5.enabled = false;
				this.btlDelFile6.enabled = false;
				this.btlDelFile7.enabled = false;
				this.btlDelFile8.enabled = false;
				this.btlDelFile9.enabled = false;
				this.btlDelFile10.enabled = false;
			}
			this.showVerBtn.label = languageUtil.getData('as-history-tit')+" [ TotalVer:"+this.allVersion+" ShowingVer:"+this.showingVersion+" ]";
		}
		
		private function keyDownEvent(keyCode:uint):void{
			if(keyCode == 27){
				if(historyPanel.visible){
					historyPanel.visible = false;
				}else{
					this.closeDialog();
				}
			}
		}